﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConcreteMappersAndObjects.DomainObjects;
using DataMappers;
using System.Data.SqlClient;
using System.Data;


namespace ConcreteMappersAndObjects.Mappers
{
    public class CUSemesterDataMapper : AbstractDataMapper<CUSemesterKey, CUSemester>
    {
        private IEntityMapper<int, CUnit> _cunitsMapper;
        private IEntityMapper<int, Semester> _semestersMapper;

        public void SetCUnitsMapper(IEntityMapper<int, CUnit> mapper)
        {
            _cunitsMapper = mapper;
        }

        public void SetSemestersMapper(IEntityMapper<int, Semester> mapper)
        {
            _semestersMapper = mapper;
        }
        protected override CUSemesterKey GetKeyFrom(System.Data.SqlClient.SqlDataReader sdr)
        {
            return new CUSemesterKey(sdr.GetInt32(0), sdr.GetInt32(1));
        }

        protected override string DoGetAllStatement()
        {
            return "select CUnitID, SemesterID from CUSemester";
        }

        protected override string DoGetFindStatement()
        {
            return DoGetAllStatement() + " where CUnitID = @CUnitID and SemesterID = @SemesterID";
        }

        protected override void BindFindStatement(System.Data.SqlClient.SqlCommand cmd, CUSemesterKey key)
        {
            cmd.Parameters.AddWithValue("@CUnitID", key.CUKey);
            cmd.Parameters.AddWithValue("@SemesterID", key.SemesterKey);
        }

        protected override CUSemester DoLoad(System.Data.SqlClient.SqlDataReader sdr)
        {
            CUSemesterKey key = GetKeyFrom(sdr);
            ConcreteValueHolder<int, CUnit> cunit = new ConcreteValueHolder<int, CUnit>(key.CUKey, _cunitsMapper);
            ConcreteValueHolder<int, Semester> semester = new ConcreteValueHolder<int, Semester>(key.SemesterKey, _semestersMapper);
            return new CUSemester(key, cunit, semester);
        }

        protected override string GetUpdateCommand()
        {
            throw new NotImplementedException();
        }

        protected override void DoBindUpdate(System.Data.SqlClient.SqlCommand cmd, CUSemester t)
        {
            throw new NotImplementedException();
        }

        protected override string GetInsertCommand()
        {
            return "insert into CUSemester (CUnitID, SemesterID) values (@CUnitID, @SemesterID)";
        }

        protected override void DoBindInsert(SqlCommand cmd, CUSemester t)
        {
            cmd.Parameters.AddWithValue("@CUnitID", t.CUnit.ID);
            cmd.Parameters.AddWithValue("@SemesterID", t.Semester.ID);
        }

        protected override string GetRemoveCommand()
        {
            return "delete from CUSemester where CUnitID = @CUnitID and SemesterID = @SemesterID";
        }

        protected override void DoBindRemove(System.Data.SqlClient.SqlCommand cmd, CUSemester t)
        {
            cmd.Parameters.Add("@CUnitID", SqlDbType.Int).Value = t.ID.CUKey;
            cmd.Parameters.Add("@SemesterID", SqlDbType.Int).Value = t.ID.SemesterKey;
        }

        protected override void FinishUpdateObject(CUSemester t)
        {
            
        }

        protected override void FinishNewObject(CUSemester t, object id)
        {
            t.ID = new CUSemesterKey(t.CUnit.ID, t.Semester.ID);
        }

        protected override void FinishDeleteObject(CUSemester t)
        {
            
        }
    }
}
